//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace SFWK.Core.Error
{
    public class OperationException<TDetail> : System.ServiceModel.FaultException<TDetail> where TDetail : SFWK.Core.SFWKError, new()
    {
        public OperationException() 
            : base( new TDetail(), new System.ServiceModel.FaultReason( typeof(TDetail).ToString() ))
        {
        }

        public OperationException(TDetail detail)
            : base(detail, new System.ServiceModel.FaultReason(typeof(TDetail).ToString()))
        {
        }
        public OperationException<TDetail> AddParam(string aParam)
        {
            this.Detail.AddParam(aParam);
            return this;
        }

        /// <summary>
        /// Throws a service exception
        /// Label to be translated can be passed in the param list as follow:
        /// OperationException<ObjectNotSaved>.ThrowException(new string[] { shipment.Reference, CMAConstants.ErrorMessage.SumRealesedQteUnderSumUnrealesedQty });
        /// </summary>
        /// <param name="paramList"></param>
        public static void ThrowException( string[] paramList ) {
            TDetail detail = new TDetail();
            detail.Parameters.AddRange(paramList);

            throw new OperationException<TDetail>( detail );
        }

        /// <summary>
        /// Throws a service exception
        /// Label to be translated can be passed in the param list as follow:
        /// OperationException<ObjectNotSaved>.ThrowException(new string[] { shipment.Reference, CMAConstants.ErrorMessage.SumRealesedQteUnderSumUnrealesedQty });
        /// </summary>
        /// <param name="paramList"></param>
        public static void ThrowException(Exception innerException, string[] paramList)
        {
            TDetail detail = new TDetail();
            detail.InnerException = innerException;
            detail.Parameters.AddRange(paramList);

            throw new OperationException<TDetail>(detail);
        }

        /// <summary>
        /// ThrowException
        /// </summary>
        public static void ThrowException()
        {
            TDetail detail = new TDetail();
            throw new OperationException<TDetail>(detail);
        }

        /// <summary>
        /// ThrowException with Variable ErrorCode
        /// </summary>
        /// <param name="ErrorCode"></param>
        /// <param name="paramList"></param>
        public static void ThrowException(string ErrorCode, string[] paramList)
        {
            TDetail detail = new TDetail();
            detail.ErrorCode = ErrorCode;
            detail.Parameters.AddRange(paramList);

            throw new OperationException<TDetail>(detail);
        }

        /// <summary>
        /// ThrowException with Variable ErrorCode
        /// </summary>
        /// <param name="ErrorCode"></param>
        public static void ThrowException(string ErrorCode)
        {
            TDetail detail = new TDetail();
            detail.ErrorCode = ErrorCode;
            throw new OperationException<TDetail>(detail);
        }

        public static void ThrowException(Exception innerException)
        {
            TDetail detail = new TDetail();
            detail.InnerException = innerException;
            throw new OperationException<TDetail>(detail);
        }
    }
}

